package com.missilike.api.controller;

import com.alibaba.fastjson.JSON;
import com.missilike.biz.IMilAccountService;
import com.missilike.core.domain.AccountLandingRequest;
import com.missilike.core.domain.AccountPsdResetRequest;
import com.missilike.core.domain.AccountRegisteredReuqest;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.IIMService;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.LoginResponse;
import com.missilike.repository.entity.MilAccount;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/access")
public class LoginController {

    @Autowired
    private IMilAccountService accountService;

    @Autowired
    private IIMService iimService;

    @ApiOperation(value = "login", notes = "登陆")
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult login(@RequestBody AccountLandingRequest request) {
        log.info("Start to execute /access/login body request={}", JSON.toJSON(request));
        MilAccount milAccount = accountService.login(request.getAccount(), request.getPassword(), request.getSmsCode(), request.getPhoneNumber());
        log.info("Completed execute /access/login responese={}", JSON.toJSON(milAccount));
        String  imSign = iimService.getUserSign(milAccount.getAccount());
        return ResultUtils.ok(new LoginResponse(milAccount,imSign));
    }

    @ApiOperation(value = "userRegistered", notes = "注册")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "thirdType", name = "thirdType", value = "第三方账号类型 1微信 2微博 3QQ", required = false)
    })
    @RequestMapping(value = "/userRegistered", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult userRegistered(@RequestBody AccountRegisteredReuqest request) {
        log.info("Start to execute /access/userRegistered body request={}", request);
        MilAccount milAccount = accountService.userRegistered(request);
        log.info("Completed execute /access/userRegistered responese={}", milAccount);
        String imSign = iimService.getUserSign(milAccount.getAccount());
        return ResultUtils.ok(new LoginResponse(milAccount,imSign));
    }

    //找回密码
    @ApiOperation(value = "userRetrievePsd", notes = "找回密码")
    @RequestMapping(value = "/userRetrievePsd", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult userRetrievePsd(@RequestBody AccountPsdResetRequest request) {
        log.info("Start to execute /access/userRetrievePsd body request={}");
        MilAccount milAccount = accountService.accountPsdResetService(request.getPhoneNumber(), request.getSmsCode(), request.getNewPassword());
        log.info("Completed execute /access/userRetrievePsd responese={}", milAccount);
        String imSign = iimService.getUserSign(milAccount.getAccount());
        return ResultUtils.ok(new LoginResponse(milAccount,imSign));
    }

}
